import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mall/constant/app_dimens.dart';
import 'package:mall/constant/app_strings.dart';
import 'package:mall/constant/text_style.dart';
import 'package:mall/modules/model/collection_entity.dart';
import 'package:mall/modules/sc/collect_sc.dart';
import 'package:mall/ui/widgets/cached_image.dart';
import 'package:mall/utils/list_placeholder_component/list_placeholder_component.dart';
import 'package:mall/router/navigator_util.dart';
import 'package:mall/utils/refresh/refresh_widget.dart';
import 'package:mall/utils/refresh/warp_refresh_controller.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class CollectPage extends StatefulWidget {
  @override
  _CollectPageState createState() => _CollectPageState();
}

class _CollectPageState extends State<CollectPage> {
  // CollectViewModel _collectViewModel = CollectViewModel();
  // RefreshController _refreshController = RefreshController();
  // int _pageIndex = 1;
  // int _pageSize = 10;

  final sc_collect = Get.put(CollectListSc());
  late final _refreshController = WarpRefreshController(loadState: sc_collect.loadState);

  // @override
  // void initState() {
  //   super.initState();
  //   _collectViewModel.queryData(_pageIndex, _pageSize);
  // }
  @override
  void dispose() {
    super.dispose();
    _refreshController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(AppStrings.MINE_COLLECT),
          centerTitle: true,
        ),
        body: _contentView());
  }

  // Widget _initView(CollectViewModel collectViewModel) {
  //   if (_collectViewModel.pageState == PageState.hasData) {
  //     return _contentView(collectViewModel);
  //   }
  //   return ViewModelStateWidget.stateWidgetWithCallBack(collectViewModel, _onRefresh);
  // }

  Widget _contentView() {
    return Container(
      margin: EdgeInsets.only(left: 15, right: 15),
      // child: SmartRefresher(
      //   enablePullDown: true,
      //   onRefresh: () => _onRefresh(),
      //   onLoading: () => _onLoadMores(),
      //   enablePullUp: collectViewModel.canLoadMore,
      //   header: WaterDropMaterialHeader(
      //     backgroundColor: AppColors.COLOR_FF5722,
      //   ),
      //   controller: _refreshController,
      //   child: GridView.builder(
      //       itemCount: collectViewModel.data.length,
      //       gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
      //         crossAxisCount: 2,
      //         childAspectRatio: 0.8,
      //       ),
      //       itemBuilder: (BuildContext context, int index) {
      //         return getGoodsItemView(collectViewModel.data[index], index);
      //       }),
      // )
      child: Obx(
        () => RefreshComponent(
          controller: _refreshController,
          onLoading: () => sc_collect.loadMoreData(),
          onRefresh: () => sc_collect.refreshData(),
          child: WrappedListPlaceholderComponent.widget(
            sc: sc_collect,
            builder: (p0) => GridView.builder(
                itemCount: p0.length,
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 2,
                  childAspectRatio: 0.8,
                ),
                itemBuilder: (BuildContext context, int index) {
                  return getGoodsItemView(p0[index], index);
                }),
          ),
        ),
      ),
    );
  }

  Widget getGoodsItemView(CollectionList collect, int index) {
    return GestureDetector(
      child: Card(
        child: Container(
            alignment: Alignment.center,
            child: Column(
              children: <Widget>[
                CachedImageView(
                  ScreenUtil().setHeight(AppDimens.DIMENS_400),
                  ScreenUtil().setHeight(AppDimens.DIMENS_400),
                  collect.picUrl,
                ),
                Container(
                  margin: EdgeInsets.only(top: 10, left: 10, right: 10),
                  child: Text(
                    collect.name ?? "",
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: FMTextStyle.color_222222_size_13,
                  ),
                ),
                Padding(padding: EdgeInsets.only(top: 10)),
                Text(
                  "${AppStrings.DOLLAR}${collect.retailPrice}",
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: FMTextStyle.color_ff5722_size_13,
                ),
              ],
            )),
      ),
      onTap: () => _itemClick(collect.valueId),
      onLongPress: () => _showDeleteDialog(collect, index),
    );
  }

  _itemClick(int? goodsId) {
    NavigatorUtil.goGoodsDetails(context, goodsId);
  }

  _showDeleteDialog(CollectionList collect, int index) {
    showDialog(
        context: context,
        barrierDismissible: true,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text(
              AppStrings.TIPS,
              style: FMTextStyle.color_333333_size_15,
            ),
            content: Text(
              AppStrings.MINE_CANCEL_COLLECT,
              style: FMTextStyle.color_222222_size_13,
            ),
            actions: <Widget>[
              TextButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: Text(
                    AppStrings.CANCEL,
                    style: FMTextStyle.color_999999_size_13,
                  )),
              TextButton(
                  onPressed: () {
                    Navigator.pop(context);
                    //_cancelCollect(collect.valueId, index);
                  },
                  child: Text(
                    AppStrings.CONFIRM,
                    style: FMTextStyle.color_ff5722_size_13,
                  )),
            ],
          );
        });
  }

  // _onRefresh() {
  //   _pageIndex = 1;
  //   _collectViewModel.queryData(_pageIndex, _pageSize);
  // }

  // _onLoadMores() {
  //   _collectViewModel.queryData(_pageIndex, _pageSize);
  // }
}
